package com.songlanyun.modules.notifytemp.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.notifytemp.model.dto.WxNotifyDTO;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.wechat.entity.TemplateData;
import com.songlanyun.modules.wechat.service.SendWxMessage;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.notifytemp.dao.NotifyTempDao;
import com.songlanyun.modules.notifytemp.entity.NotifyTempEntity;
import com.songlanyun.modules.notifytemp.service.NotifyTempService;

import javax.annotation.Resource;


@Service("yxtNotifyTempService")
public class NotifyTempServiceImpl extends ServiceImpl<NotifyTempDao, NotifyTempEntity> implements NotifyTempService {


    @Resource
    private SendWxMessage sendWxMessage;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title = MapUtil.getStr(params, "title");//标题
        String content = MapUtil.getStr(params, "content");//内容
        Integer used = MapUtil.getInt(params, "used");//用途
        Integer wxType = MapUtil.getInt(params, "wx_type");//模板类型
        IPage<NotifyTempEntity> page = this.page(
                new Query<NotifyTempEntity>().getPage(params),
                new LambdaQueryWrapper<NotifyTempEntity>()
                        .like(StringUtils.isNotBlank(title), NotifyTempEntity::getTitle, title)
                        .like(StringUtils.isNotBlank(content), NotifyTempEntity::getContent, content)
                        .like(used != null, NotifyTempEntity::getUsed, used)
                        .like(wxType != null, NotifyTempEntity::getWxType, wxType)
                        .orderByDesc(NotifyTempEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    /**
     * 发送微信小程序消息
     */
    @Override
    public void sendWxNotify(WxNotifyDTO dto) {
        ValidatorUtils.validateEntity(dto);

        //根据微信类型查询微信消息的模板
        NotifyTempEntity notifyTemp = this.baseMapper.selectOne(
                new LambdaQueryWrapper<NotifyTempEntity>()
                        .eq(NotifyTempEntity::getIsDelete, Constant.STET)
                        .eq(NotifyTempEntity::getIsUse, Constant.ENABLE)
                        .eq(NotifyTempEntity::getWxType, dto.getWxType())
                        .last("limit 1")
        );
        if (notifyTemp == null) {
            throw new RRException("没有找到微信消息模板");
        }

        //微信详细内容字段
        String wxField = notifyTemp.getWxField();
        String[] fields = wxField.split(",");
        if (fields.length != dto.getValues().size()) {
            throw new RRException("微信详细内容字段与值不对应,发送微信消息失败");
        }

        //模板字段与模板对应的值
        Map<String, TemplateData> map = new LinkedHashMap<>(fields.length);
        //对应赋值
        for (int i = 0; i < fields.length; i++) {
            map.put(fields[i], new TemplateData(dto.getValues().get(i)));
        }

        //发送微信消息
        for (String openId : dto.getOpenIds()) {
            sendWxMessage.push(openId, notifyTemp.getWxTempid(), map);
        }
    }

    @Override
    public NotifyTempEntity getIdByType(Integer type) {
        //根据微信类型查询微信消息的模板
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<NotifyTempEntity>()
//                        .eq(NotifyTempEntity::getIsDelete, Constant.STET)
                        .eq(NotifyTempEntity::getIsUse, Constant.ENABLE)
                        .eq(NotifyTempEntity::getWxType, type)
                        .last("limit 1")
        );


    }


}
